package com.ala4.oxcafe.controller;

import com.ala4.oxcafe.boot.page.PageDomain;
import com.ala4.oxcafe.boot.page.PageResult;
import com.ala4.oxcafe.domain.R;
import com.ala4.oxcafe.dto.RoleMenuRelDTO;
import com.ala4.oxcafe.entity.RoleInfo;
import com.ala4.oxcafe.service.RoleInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色信息controller
 *
 * @author PING
 * @version 1.0.0
 * @date 2025/8/12 14:20
 */
@RestController
@RequestMapping("/role-info")
@AllArgsConstructor
@Tag(name = "角色管理", description = "用于管理角色信息")
public class RoleInfoController {

    private final RoleInfoService roleInfoService;

    @Operation(summary = "分页查询角色信息")
    @GetMapping("/page")
    public R<PageResult<RoleInfo>> page(PageDomain pageDomain, RoleInfo roleInfo) {
        return R.ok(PageResult.iPage(roleInfoService.userInfoPage(pageDomain, roleInfo)));
    }

    @Operation(summary = "列表查询角色信息")
    @GetMapping("/list")
    public R<List<RoleInfo>> list(RoleInfo roleInfo) {
        return R.ok(roleInfoService.userInfoList(roleInfo));
    }

    @Operation(summary = "创建角色")
    @PostMapping("/create")
    public R<RoleInfo> create(@RequestBody @Validated(RoleInfo.Create.class) RoleInfo roleInfo) {
        return R.ok(roleInfoService.createRoleInfo(roleInfo));
    }

    @Operation(summary = "更新角色")
    @PutMapping("/update")
    public R<RoleInfo> update(@RequestBody @Validated(RoleInfo.Update.class) RoleInfo roleInfo) {
        return R.ok(roleInfoService.updateRoleInfo(roleInfo));
    }

    @Operation(summary = "删除角色")
    @DeleteMapping("/delete/{roleId}")
    public R<?> delete(@PathVariable Long roleId) {
        roleInfoService.delete(roleId);
        return R.ok();
    }

    @Operation(summary = "角色详情")
    @GetMapping("/{roleId}")
    public R<RoleInfo> info(@PathVariable Long roleId) {
        return R.ok(roleInfoService.roleInfo(roleId));
    }

    @Operation(summary = "角色分配菜单")
    @PutMapping("/allocation")
    public R<?> allocation(@RequestBody @Validated RoleMenuRelDTO roleMenuRelDTO) {
        roleInfoService.allocation(roleMenuRelDTO);
        return R.ok();
    }
}
